Interview Questions

1. File Owners

Implement a get_file_ownership function
import os
from pwd import getpwuid
from grp import getgrgid

def get_file_ownership(filename):
    ''' Get owner user and group information
    '''
    return (
        getpwuid(os.stat(filename).st_uid).pw_name,
        getgrgid(os.stat(filename).st_gid).gr_name
    )

2. Maximum Product of Three Numbers

Given an integer array, find three numbers whose product
is maximum and output the maximum product.
Example 1:
Input: [1,2,3]
Output: 6
Example 2:
Input: [1, 2,3, 4]
Output: 24
Note:
The length of the given array will be in range [3,104]
and all elements are in the range [-1000, 1000].
Multiplication of any three numbers in the input will not
exceed the range of 32-bit signed integer.
class Solution(object):

   def maximumProduct(self, nums):
       """
       :type nums: List[int]
       :rtype: int
       """

       max1 = max(nums)    # O(n)
       nums.remove(max1)

       max2 = max(nums)
       nums.remove(max2)

       max3 = max(nums)
       nums.remove(max3)

       min1 = max3
       min2 = max2

       if len(nums) > 0:
           min1 = min(nums)
           nums.remove(min1)

       if len(nums) > 0:
           min2 = min(nums)
           nums.remove(min2)

       return max(max1 * max2 * max3, max1 * min1 * min2)
def maximum_product(nums):

    max1 = -sys.maxsize - 1
    max2 = -sys.maxsize - 1
    max3 = -sys.maxsize - 1

    min1 = sys.maxsize
    min2 = sys.maxsize

    for num in nums:
        if max1 < num:
            max1 = num
            max2 = max1
            max3 = max2
        else:
            if max2 < num:
                max2 = num
                max3 = max2
            else:
                if max3 < num:
                    max3 = num
        if min1 > num:
            min2 = min1
            min1 = num
            continue
        if min2 > num:
            min2 = num
        # print(max3, max2, max1)
        # print(min1, min2, max1)
    return [max3 * max2 * max1 if min1 * min2 * max1 < max3 * max2 * max1 else min1 * min2 * max1]

A = [1, 2, 3, 4]
print(maximum_product(A))